// pages/analysis/analysis.js

var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showDevices: false,
    selectedDevice: {},
    totalDevices: {},
    tenhouses: [],
    dataList: [],
    showLoading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //检测是否登录
    if (!app.globalData.userInfo) {
      wx.navigateTo({
        url: '/pages/login/login',
      });
    };
    var henscount = wx.getStorageSync('hensCount');
    if (henscount == "") {
      wx.setStorageSync('hensCount', 10000);
      henscount = 10000;
    }

    this.setData({
      selectedDevice: app.globalData.selectedDevice,
      totalDevices: app.globalData.totalDevices,
      hensCount: henscount
    });
    this.getHenhouses();
    this.init(0);
  },

  getHenhouses() {
    var currentPage = this;
    wx.request({
      url: app.globalData.baseUrl + '/api/services/app/OneNetApiService/GetDeviceProperties',
      data: { deviceId: this.data.selectedDevice.deviceId },
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.token
      },
      success(res) {
        currentPage.setData({
          tenhouses: res.data.result
        })
      },
      fail(res) {
        console.log(res);
      },
      complete(res) {
        currentPage.setData({
          showLoading: false
        });
      }
    });
  },

  changeHenhouse(event) {
    this.init(event.detail.index)
  },

  init(index) {
    this.setData({ showLoading: true });
    var currentPage = this;
    wx.request({
      url: app.globalData.baseUrl + '/api/services/app/OneNetApiService/GetOneWeekDeviceTotalData',
      data: {
        deviceId: this.data.selectedDevice.deviceId,
        deviceTypeId: this.data.selectedDevice.deviceTypeId,
        index: index,
        henCount: this.data.hensCount
      },
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.token
      },
      success(res) {
        var source = res.data.result;
        currentPage.setData({
          dataList: source,
        });
      },
      fail(res) {
        console.log(res);
      },
      complete(res) {
        currentPage.setData({
          showLoading: false
        });
      }
    });

  },

  changeHensCount(event) {
    var henscount = wx.setStorageSync('hensCount', this.data.hensCount);
    this.init(0);
  },

  showDeviceList() {
    this.setData({ showDevices: true });
  },
  closeDeviceList() {
    this.setData({ showDevices: false });
  },
  changeSelectedDevice(e) {
    var selected = e.currentTarget.dataset["item"];
    this.setData({
      selectedDevice: selected,
      showDevices: false
    });
    this.init(0)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})